package com.kuaifu.autofare.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.kuaifu.autofare.bean.model.ParkInfo;
import com.kuaifu.autofare.bean.model.ParkingEnterprise;
import com.kuaifu.autofare.bean.vo.req.UserReqVo;
import com.kuaifu.autofare.bean.vo.resp.ParkDailyTransferDetailRespVo;
import com.kuaifu.autofare.bean.vo.resp.ParkTransDetailRespVo;
import com.kuaifu.autofare.bean.vo.resp.ParkTransTotalRespVo;
import com.kuaifu.autofare.common.utils.EncryptUtil;
import com.kuaifu.autofare.common.utils.LogUtils;
import com.kuaifu.autofare.common.utils.ResultMapUtil;
import com.kuaifu.autofare.common.web.BaseController;
import com.kuaifu.autofare.service.IParkInfoService;
import com.kuaifu.autofare.service.IParkingEnterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kuaifu.autofare.bean.model.ParkingLotCustomer;
import com.kuaifu.autofare.common.utils.StringUtils;
import com.kuaifu.autofare.service.IParkingLotCustomerService;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/parkingLotCustomer")
public class ParkingLotCustomerController extends BaseController {
    @Autowired
    private IParkingLotCustomerService parkingLotCustomerService;

    @Autowired
    private IParkingEnterpriseService parkingEnterpriseService;

    @Autowired
    private IParkInfoService parkInfoService;

    /**
     * 商家的绑定商家账号页面
     *
     * @param
     * @return
     * @throws Exception
     */
    @RequestMapping("page/bindWx")
    public ModelAndView pageBindWx(String openId, String appId) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/shop/register");
        mv.addObject("openId", openId);
        mv.addObject("appId", appId);

        JSONObject params = new JSONObject();
        params.put("uri", "/parkingLotCustomer/bindWx");
        params.put("openId", openId);
        params.put("appId", appId);
        mv.addObject("bindWxParams", EncryptUtil.encrypt(params.toJSONString()));

        JSONObject enterpriseParams = new JSONObject();
        enterpriseParams.put("uri", "/parkingLotCustomer/getParkingEnterprise");
        enterpriseParams.put("openId", openId);
        enterpriseParams.put("appId", appId);
        mv.addObject("enterpriseParams", EncryptUtil.encrypt(enterpriseParams.toJSONString()));

        return mv;
    }

    /**
     * 绑定商家账号
     *
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("bindWx")
    public Map<String, Object> bindWx(ParkingEnterprise parkingEnterprise) {
        Map<String, String> resultMap = null;
        try {
            LogUtils.debug(JSONObject.toJSONString(parkingEnterprise));
            resultMap = parkingEnterpriseService.bindWx(parkingEnterprise);
            if (StringUtils.isBank(resultMap.get("error"))) {
                return ResultMapUtil.genDefaultSuccessMap();
            } else {
                return ResultMapUtil.genErrorMap(resultMap.get("error"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMapUtil.genErrorMap("删除失败");
        }
    }

    /**
     * 解除绑定商家账号
     *
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("unBindWx")
    public Map<String, Object> unBindWx(ParkingEnterprise parkingEnterprise) {
        try {
            LogUtils.debug(JSONObject.toJSONString(parkingEnterprise));
            boolean flag = parkingEnterpriseService.unBindWx(parkingEnterprise);
            if (flag) {
                return ResultMapUtil.genDefaultSuccessMap();
            } else {
                return ResultMapUtil.genErrorMap("");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMapUtil.genErrorMap("删除失败");
        }
    }

    /**
     * 获取商家账号
     *
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("getParkingEnterprise")
    public ModelAndView getParkingEnterprise(int custId) throws Exception {
        ModelAndView mv = new ModelAndView();

        ParkingEnterprise parkingEnterprise = parkingEnterpriseService.getByCustId(custId);
        ParkingLotCustomer parkingLotCustomer = parkingEnterprise.getParkingLotCustomer();
        mv.addObject("parkingName", parkingLotCustomer.getName());
        mv.setViewName("/shop/center");
        mv.addObject("custId", custId);

        JSONObject params = new JSONObject();
        params.put("uri", "/parkingLotCustomer/unBindWx");
        params.put("custId", custId);
        mv.addObject("unBindWxParams", EncryptUtil.encrypt(params.toJSONString()));

        JSONObject bindWxPageParams = new JSONObject();
        bindWxPageParams.put("uri", "/parkingLotCustomer/page/bindWx");
        bindWxPageParams.put("openId", parkingEnterprise.getOpenId());
        bindWxPageParams.put("appId", parkingEnterprise.getAppId());
        mv.addObject("bindWxPageParams", EncryptUtil.encrypt(bindWxPageParams.toJSONString()));

        return mv;
    }

    /**
     * 商家的收入流水页面
     *
     * @param custId
     * @return
     * @throws Exception
     */
    @RequestMapping("transTotal")
    public ModelAndView transTotal(int custId, String date) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/revenue/transTotal");
        mv.addObject("custId", custId);

        //页面跳转：查看详细收入流水
        JSONObject transTotalParams = new JSONObject();
        transTotalParams.put("uri", "/parkingLotCustomer/getTransTotal");
        transTotalParams.put("custId", custId);
        mv.addObject("transTotalParams", EncryptUtil.encrypt(transTotalParams.toJSONString()));

        //页面跳转：查看详细收入流水
        JSONObject transDetailParams = new JSONObject();
        transDetailParams.put("uri", "/parkingLotCustomer/transDetail");
        transDetailParams.put("custId", custId);
        mv.addObject("transDetailParams", EncryptUtil.encrypt(transDetailParams.toJSONString()));

        //页面跳转：查看慧付单收入到账情况
        JSONObject transferDetailParams = new JSONObject();
        transferDetailParams.put("uri", "/parkingLotCustomer/transferDetail");
        transferDetailParams.put("custId", custId);
        mv.addObject("transferDetailParams", EncryptUtil.encrypt(transferDetailParams.toJSONString()));


        ParkTransTotalRespVo parkTransTotalRespVo = parkingEnterpriseService.getTransTotal(custId, date);
        LogUtils.debug(JSONObject.toJSONString(parkTransTotalRespVo));
        mv.addObject("parkTransTotalRespVo", parkTransTotalRespVo);
        return mv;
    }

    /**
     * 异步获取交易总额
     *
     * @param custId
     * @param date
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("getTransTotal")
    public Map<String, Object> getTransTotal(int custId, String date) throws Exception {
        try {
            ParkTransTotalRespVo parkTransTotalRespVo = parkingEnterpriseService.getTransTotal(custId, date);

            if (parkTransTotalRespVo != null) {
                return ResultMapUtil.genSuccessDataMap(parkTransTotalRespVo);
            } else {
                return ResultMapUtil.genErrorMap("");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMapUtil.genErrorMap("获取数据失败");
        }
    }

    /**
     * 商家的收入流水页面
     *
     * @param custId
     * @return
     * @throws Exception
     */
    @RequestMapping("transDetail")
    public ModelAndView transDetail(int custId, String date) throws Exception {

        ModelAndView mv = new ModelAndView();
        mv.setViewName("/revenue/transDetail");
        mv.addObject("custId", custId);
        if (!StringUtils.isBank(date)) {
            mv.addObject("date", date);
        }


        ParkTransDetailRespVo parkTransDetailRespVo = parkingEnterpriseService.getTransDetail(custId, date);
        LogUtils.debug(JSONObject.toJSONString(parkTransDetailRespVo.getCashList()));
        mv.addObject("cashList", parkTransDetailRespVo.getCashList());
        LogUtils.debug(JSONObject.toJSONString(parkTransDetailRespVo.getPlatformList()));
        mv.addObject("platformList", parkTransDetailRespVo.getPlatformList());

        JSONObject transDetailParams = new JSONObject();
        transDetailParams.put("uri", "/parkingLotCustomer/transDetail");
        transDetailParams.put("custId", custId);
        mv.addObject("transDetailParams", EncryptUtil.encrypt(transDetailParams.toJSONString()));

        return mv;
    }

    /**
     * 惠付单的收入流水页面
     *
     * @param custId
     * @return
     * @throws Exception
     */
    @RequestMapping("transferDetail")
    public ModelAndView transferDetail(int custId) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/revenue/transferDetail");
        mv.addObject("custId", custId);

        //页面跳转：查看详细收入流水
        JSONObject transDetailParams = new JSONObject();
        transDetailParams.put("uri", "/parkingLotCustomer/transDetail");
        transDetailParams.put("custId", custId);
        transDetailParams.put("checkPlatform", custId);
        mv.addObject("transDetailParams", EncryptUtil.encrypt(transDetailParams.toJSONString()));

        //收费方式（0-未支付 1-现金 2-平台代扣）
        List<ParkDailyTransferDetailRespVo> voList = parkInfoService.getDailyTransfer(custId, 2);
        mv.addObject("transDetails", voList);
        return mv;
    }
}
